package com.hxl.dailyworksummary.settings;

import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class AppSettingsConfigurable implements Configurable {
    private AppSettingsComponent settingsComponent;

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Daily Work Summary";
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return settingsComponent.getPanel();
    }

    @Override
    public @Nullable JComponent createComponent() {
        settingsComponent = new AppSettingsComponent();
        return settingsComponent.getPanel();
    }

    @Override
    public boolean isModified() {
        SettingsState settings = SettingsState.getInstance();
        boolean modified = false;
        modified = !settingsComponent.getGitUsername().equals(settings.getState().gitUsername);
        modified = modified || !settingsComponent.getGitPassword().equals(settings.getState().gitPassword);
        modified = modified || !settingsComponent.getAiToken().equals(settings.getState().aiToken);
        return modified;
    }

    @Override
    public void apply() {
        SettingsState settings = SettingsState.getInstance();
        assert settings.getState() != null;
        settings.getState().gitUsername = settingsComponent.getGitUsername();
        settings.getState().gitPassword = settingsComponent.getGitPassword();
        settings.getState().aiToken = settingsComponent.getAiToken();
    }

    @Override
    public void reset() {
        SettingsState settings = SettingsState.getInstance();
        assert settings.getState() != null;
        settingsComponent.setGitUsername(settings.getState().gitUsername);
        settingsComponent.setGitPassword(settings.getState().gitPassword);
        settingsComponent.setAiToken(settings.getState().aiToken);
    }

    @Override
    public void disposeUIResources() {
        settingsComponent = null;
    }
}